﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


using System;

namespace Marqdown
{
    namespace StateMachine
    {
        public class LinkFootnote : CompetitiveState
        {
            //internal static State hrLeading = new HROverridden.LeadingChar();
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Absorb
                        break;
                    case '[':
                        // Next State
                        info.linkString.Length = 0;
                        info.ChangeState(LinkLabel.linkLabel);
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = " ";
                a.action = Info.nop;
            }
        }
        public class LinkLabel : CompetitiveState
        {
            internal static ConcreteState linkLabel = new LinkLabel();
            internal static ConcreteState linkCloseBracket = new LinkCloseBracket();
            internal static ConcreteState linkColon = new LinkColon();
            internal static ConcreteState preLinkURL = new PreLinkURL();
            internal static ConcreteState linkURL = new LinkURL();
            internal static ConcreteState linkPreTitle = new LinkPreTitle();
            internal static ConcreteState linkTitle = new LinkTitle();
            internal static ConcreteState linkPostTitle = new LinkPostTitle();
            internal static ConcreteState linkNewLine = new LinkNewLine();
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.competitorFailed();
                        break;
                    case ']':
                        // Next State if label length>0
                        if (info.linkString.Length > 0)
                        {
                            info.linkItem.label = info.linkString.ToString();
                            info.ChangeState(LinkLabel.linkCloseBracket);
                        }
                        else
                        {
                            info.competitorFailed();
                        }
                        break;
                    default:
                        // Log label chars
                        info.linkString.Append(c);
                        break;
                }
            }
            public override void GetAction(ActionSetting a)
            {
                a.leading = "[";
                a.action = Info.nop;
            }
        }
        public class LinkCloseBracket : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ':':
                        info.ChangeState(LinkLabel.linkColon);
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }
        }
        public class LinkColon : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.ChangeState(LinkLabel.preLinkURL);
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }
        }
        public class PreLinkURL : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Absorb
                        break;
                    case '\n':
                        info.competitorFailed();
                        break;
                    default:
                        info.linkString.Length = 0;
                        info.linkString.Append(c);
                        info.ChangeState(LinkLabel.linkURL);
                        break;
                }
            }
        }
        public class LinkURL : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.linkItem.url = info.linkString.ToString();
                        //info.AddToLinkMap();
                        // Next State
                        info.ChangeState(LinkLabel.linkPreTitle);
                        break;
                    case '\n':
                        info.linkItem.url = info.linkString.ToString();
                        info.AddToLinkMap();
                        info.ChangeState(LinkLabel.linkNewLine);
                        break;
                    default:
                        // Log url chars
                        info.linkString.Append(c);
                        break;
                }
            }
        }
        public class LinkPreTitle : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Absorb
                        break;
                    // Could allow newline
                    case '\"':
                    case '\'':
                        // Next State
                        info.linkString.Length = 0;
                        info.ChangeState(LinkLabel.linkTitle);
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }
        }
        public class LinkTitle : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\"':
                    case '\'':
                        // Next State
                        info.linkItem.title = info.linkString.ToString();
                        info.AddToLinkMap();
                        info.ChangeState(LinkLabel.linkPostTitle);
                        break;
                    case '\n':
                        info.competitorFailed();
                        break;
                    default:
                        // Log title chars
                        info.linkString.Append(c);
                        break;
                }
            }
        }
        public class LinkPostTitle : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Absorb
                        break;
                    case '\n':
                        info.ChangeState(LinkLabel.linkNewLine);
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }
        }
        public class LinkNewLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        // Absorb
                        break;
                    default:
                        ///This is the new Success for a CompetitiveState
                        info.ResendAsLeading(c);
                        break;
                }
            }
            public override void terminate(Info info)
            {
                //System.Console.WriteLine("LinkNewLine.terminate");
                //info
                //info.tag.NewLine();
            }
        }



    }
}
